package listbyorder.access001_100.test59;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/2 9:01
 */
public class Solution1 {

    int count = 1;

    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        dfs(res, 0, 0 ,n - 1, n - 1);
        return res;
    }

    private void dfs(int[][] res, int sr, int sc, int er, int ec) {
        if(sr > er) return;
        if(sr == er){
            res[sr][sc] = count++;
            return;
        }
        for (int i = sc; i < ec; i++) {
            res[sr][i] = count++;
        }
        for (int i = sr; i < er; i++) {
            res[i][ec] = count++;
        }
        for (int i = ec; i > sc; i--) {
            res[er][i] = count++;
        }
        for (int i = er; i > sr ; i--) {
            res[i][sc] = count++;
        }
        dfs(res, sr + 1, sc + 1, er - 1, ec - 1);
    }
}
